import { Component, OnInit } from '@angular/core';
import {ExtendHttpService} from "../../../common/extend-http.service";
import {TipService} from "../../services/tip.service";
import {Router} from "@angular/router";

@Component({
  selector: 'app-line-up-submit',
  templateUrl: './line-up-submit.component.html',
  styleUrls: ['./line-up-submit.component.css']
})
export class LineUpSubmitComponent implements OnInit {

  tipType = 'success';
  isLoading = false;
  list = [];
  selectId = '';
  constructor(
    private extendHttp: ExtendHttpService,
    private tipService: TipService,
    private router: Router
  ) { }

  ngOnInit() {
    cBack =()=> {
      $('#btn-back').trigger('click');
    }
    this.getData();
  }

  onSubmit() {
    if (!this.isLoading) {
    if (!this.selectId.trim()) {
      this.tipType = 'error';
      this.tipService.change(true, '请选择取号分类');
      return;
    }

    this.isLoading = !this.isLoading;
    this.extendHttp.post('/merchantApp/call/add',  {id: this.selectId}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.tipType = 'success';
        this.selectId = '';
        this.isLoading = !this.isLoading;
        this.tipService.change(true, data.message);
      } else if (data.code == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(()=> {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, data.message);
        this.isLoading = !this.isLoading;
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
      this.isLoading = !this.isLoading;
    });
    }
  }

  selectItem(id) {
    this.selectId = id;
  }
  getData() {
    this.extendHttp.post('/merchantApp/call/getCallList', {}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.list = data.list;
      } else if (data.code == '-2001') {
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

}
